<style type="text/css">
	#form-share{
		margin-top: 20px;
		width: 80%;
	}
</style>
<script type="text/html" id="tpl-file_share">
	<div class="layui-form" id='form-share' action="">
		<div class="layui-form-item">
			<label class="layui-form-label">网址</label>
			<div class="layui-input-block">
				<input type="text" id="{{d.elem}}_url" readonly="readonly" class="layui-input" value="--loading--">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">提取码</label>
			<div class="layui-input-inline">
				<input type="text" id="{{d.elem}}_password" readonly="readonly" lay-verify="required" placeholder="请输入四位提取码"
					class="layui-input">
			</div>
			<!-- <div class="layui-form-mid layui-word-aux">可编辑</div> -->
		</div>
		
		<div class="layui-form-item layui-hide">
			<label class="layui-form-label">有效期</label>
			<div class="layui-input-block">
				<select name="city" lay-verify="required">
					<option value="0">永久</option>
					<option value="1">下载后失效</option>
					<option value="2">一天</option>
					<option value="2">七天</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<!-- 复制内容标签 -->
				<input style="position: absolute;top: -200px;" id="{{d.elem}}_copy_content" type="text">
				<button class="layui-btn layui-btn-sm layui-btn-normal" id="{{d.elem}}_btn_save_copy" lay-submit lay-filter="formDemo">复制</button>
				<button class="layui-btn layui-btn-sm layui-btn-danger" id="{{d.elem}}_cancel_share" lay-submit lay-filter="formDemo">取消分享</button>
				<!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
			</div>
		</div>
	</div>
</script>
<script type="text/html" id="bar_file_share">
  <!-- <a class="layui-btn layui-btn-xs" lay-event="edit">使用</a> -->
  <a class="layui-btn layui-btn-xs" lay-event="download">下载</a>
  <!-- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> -->
</script>
<!-- 文件尺寸模板 -->
<script type="text/html" id="tpl_file_share_size">
	{{# if(d.size==0){ }}
		0 B
	{{# }else if(d.size){ }}
		{{app.bytesToSize(d.size)}}
	{{# }else{ }}
		-
	{{# } }}
</script>
<script type="text/javascript">
	app.defTpl('file_share',function(arg){
		layui.use(['layer','laytpl','form'],function(){
			var laytpl = layui.laytpl,$=layui.$;
			var form = layui.form;
			// 创建密码
			function create_pwd(pwd_length) {
				var new_pwd = "";
				var j = 0,m=0;
				var char = 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
				for (var i = 0; i < parseInt(pwd_length); i++) {
					j = Math.floor(Math.random() * char.length);
					new_pwd += char.charAt(j);
					m += j += ',';
				}
				return new_pwd;
			}
			var tplData={}
			var elem='share_95302870';
			tplData['password']= create_pwd(4);
			tplData['elem'] = elem;

			laytpl($('#tpl-file_share').html()).render(tplData, function (html) {
				var layerShareIndex=app.layer({
					type: 1,
					title: '分享文件',
					anim: 2,
					closeBtn:1,
					area: ['190px'],
					shade: 0.5,
					// shadeClose: true,
					content: html,
					success: function (o, index) {
						var postData={};
						var shareId=0;
						if(arg.only_tag){
							postData['only_tag']= arg.only_tag;
							app.ajaxPost('/api/share/getInfo', postData, function (res) {
								console.log(res)
								var domain_name = window.location.protocol + "//" + window.location.host
								$('#' + elem + '_url').val(domain_name + "/share/file/"+ res.data.only_tag);
								$('#' + elem + '_password').val(res.data.password);
								shareId=res.data.id;
							})
						}else{
							postData['fileId'] = arg.file_id;
							postData['failureType'] = 1;
							postData['status'] = 1;
							postData['password'] = tplData['password'];
							form.render()
							var url = '/api/share/create'
							if(arg.method=="read"){
								url = '/api/share/getInfo'
							}
							app.ajaxPost(url, postData, function (res) {
								console.log(res)
								var domain_name = window.location.protocol + "//" + window.location.host
								$('#' + elem + '_url').val(domain_name + "/share/file/" + res.data.only_tag);
								$('#' + elem + '_password').val(res.data.password);
								shareId=res.data.id;
							})
						}
						
						// 保存复制按钮
						$("#" + elem + '_btn_save_copy').click(function () {
							var content = $('#' + elem + '_url').val() + " 提取码：" + $('#' + elem + '_password').val();
							$("#" + elem + '_copy_content').val(content);
							$("#" + elem + '_copy_content').select();
							// console.log(content);
							document.execCommand("copy");
							layer.msg('已复制');
						})

						// 取消分享按钮
						$("#" + elem + '_cancel_share').click(function () {
							
							layer.confirm('确定后此链接将被删除，再也无法访问，确定关闭分享？', {
								btn: ['确定', '取消'] //按钮
								,title:'确定取消分享'
							}, function (index) {
								var postData = { 'id': shareId };
								app.ajaxPost('/api/share/delete', postData, function (res) {
									layer.close(layerShareIndex);
									console.log(res)
									layer.msg('已停止分享');
								})
							})
						})
						
					}
				})
			})
		})
	});
</script>